# 1_bayes_model.R
# Bayesian model using JAGS software on high-performance-computer
# July 2021
# updated with Trump County share on March 2022

# move to high-performance-computer
here = getwd()
setwd("//hpc-fs/barnetta/vaccine")

# make model file for JAGS software
source('1_model_file.R')
source('1_MCMC_parameters.R') # get the number of chains and thinning for MCMC run

## prepare the data
# need to make binary versions of key variables
model_ready = mutate(pretest_final, 
                       statenum = as.numeric(as.factor(state)),
                       poolnum = as.numeric(as.factor(pool)),
                       TrumpShare = (TrumpShare-0.5) / 0.1, # scaled to 10%
                       age_c = (age-40) / 10, # standardise age to 10 years
                       male = as.numeric(gender=='Male'),
                       trt2_c = as.numeric(as.numeric(Treatment2)==2), # cash
                       trt2_l = as.numeric(as.numeric(Treatment2)==3), # lottery
                       int1 =trt2_c*male, # make interactions
                       int2 =trt2_l*male,
                       Education = ifelse(is.na(Education)==TRUE, 'Low', Education)) # impute 11 missing

# prepare data as a list for JAGS
N = nrow(model_ready) # 
formula = clicked ~ trt2_c + trt2_l + male + age_c + Race + Education + TrumpShare 
X = model.matrix(formula, model_ready) # design matrix; includes intercept
P = ncol(X)
C = max(model_ready$poolnum)
S = max(model_ready$statenum)
bdata = with(model_ready, list(N = N, P = P, X = X, C = C, S = S, click = clicked, pool = poolnum, state = statenum))
# initial values
inits = list(alpha = rep(0, P), gamma=rep(0, C), lambda=rep(0, S), tau.gamma = 1, tau.lambda = 1)

# save for running in JAGS
parms = c('alpha', 'gamma_c', 'lambda_c')
subgroup = 'full'
outfile = paste('jags_ready_', subgroup, '.RData', sep='')
save(model.file, parms, bdata, inits, file=outfile)

# move back
setwd(here)
